/**
 * 
 */
package com.star.util.image;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

/**
 * @ClassName: ImageUtil
 * @Description: TODO
 * @author starhq
 * @createtime 2014年10月14日 上午9:16:17
 * @email : jian.wang@shinsoft.net
 * @modify by TODO
 * @resaon
 * @edittime
 * @version 1.0
 * @copyright shinsoft
 */
public class ImageUtil {

	public static boolean narrowAndFormateTransfer(InputStream input,
			String destPath, String formate) {
		boolean flag = false;
		try {
			File destFile = new File(destPath);
			if (!destFile.getParentFile().exists()) {
				destFile.getParentFile().mkdir();
			}
			FileOutputStream fos = new FileOutputStream(destFile);
			BufferedImage src = ImageIO.read(input);
			// BufferedImage src = ImageIO.read(srcFile); // 读入文件
			Image image = src.getScaledInstance(src.getWidth(),
					src.getHeight(), Image.SCALE_DEFAULT);
			BufferedImage tag = new BufferedImage(src.getWidth(),
					src.getHeight(), BufferedImage.TYPE_INT_RGB);
			Graphics g = tag.getGraphics();
			g.drawImage(image, 0, 0, null); // 绘制缩小后的图
			g.dispose();
			flag = ImageIO.write(tag, formate, fos);// 输出到文件流
			fos.close();
			input.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return flag;
	}
}
